package Jona;

/**
 * @author Xox Ix
 *
 * TODO To change the template for this generated type comment go to
 * Window - Preferences - Java - Code Style - Code Templates
 */
public class Conjunct extends Statement {
	ArgObject [] operands = new ArgObject[0];
	
	public Conjunct() {
		super(ArgObject.ISSUE, "");
	}
	
	public Conjunct(ArgObject [] operands) {
		super(ArgObject.ASSUMPTION, "");
		this.operands = operands;
	}
	
	public ArgObject[] getOperands() {
		return operands;
	}
	
	public boolean contains(ArgObject ao1)
	{
		int i;
		for (i = 0; i < operands.length; ++i) {
			if (operands[i].equals(ao1)) {
				return true;
			} else if (operands[i] instanceof Conjunct) {
				if (((Conjunct)operands[i]).contains(ao1)) {
					return true;
				}
			}
		}
		return false;
	}
	public void addOperand(ArgObject operand) {
		if (operand == null) return;
		operands = addToList(operands, operand);
	}
	
	public void removeOperand(ArgObject operand) {
		if (operand == null) return;
		operands = removeFromList(operands, operand);
	}
	
	public int evaluate(Theory th) {
		int retVal, i, valOperand;
		retVal = super.evaluate(th);;
		for (i = 0; i < operands.length; ++i) {
			retVal = Math.min(retVal, th.getValue(operands[i]));
			if (retVal == ArgObject.EVALUATION_DEFEATED || retVal == ArgObject.EVALUATION_UNASSIGNED) {
				System.out.println("Statement '"+getText()+"' defeated");
				return ArgObject.EVALUATION_DEFEATED;
			}
		}
		if (retVal == ArgObject.EVALUATION_UNDEFEATED) {
			System.out.println("Statement '"+getText()+"' justified");
		}
		return retVal;
	}
	
	public String getText() {
		int i;
		String rt;
		if (operands.length == 0) return "()";
		rt = "(" + operands[0].getText();
		for (i = 1; i < operands.length-1; ++i) {
			rt += "), (" + operands[i].getText();
		}
		if (operands.length > 1) {
			rt += ") and ("+operands[operands.length - 1].getText();
		}
		return rt + ")";
	}
}
